package com.hanrui.android.net.presenter.message;

import android.content.Context;
import android.util.Log;

import com.hanrui.android.activity.LoginActivity;
import com.hanrui.android.library.base.BaseBean;
import com.hanrui.android.library.base.BasePresenter;
import com.hanrui.android.constant.Constant;
import com.hanrui.android.model.TaskBackForm;
import com.hanrui.android.library.net.ApiService;
import com.hanrui.android.library.net.RetrofitManager;
import com.hanrui.android.library.net.RxSchedulers;
import com.hanrui.android.net.contract.message.BackTaskContract;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;


public class BackTaskPresenter extends BasePresenter<BackTaskContract.View> implements BackTaskContract.Presenter {

    @Inject
    public BackTaskPresenter(){}


    @Override
    public void taskBackForm(final Context context, String procInsId) {

        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .taskBackForm(procInsId)
                .compose(RxSchedulers.<BaseBean<TaskBackForm>>applySchedulers())
                .compose(mView.<BaseBean<TaskBackForm>>bindToLife())
                .subscribe(new Consumer<BaseBean<TaskBackForm>>() {
                    @Override
                    public void accept(BaseBean<TaskBackForm> dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.toString());
                        if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else if (dataResponse.getCode() == 1){
                            mView.taskBackForm(dataResponse.getData().getListData());
                        }else {
                            mView.showFaild(String.valueOf(dataResponse.getMessage()));
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });

    }


    @Override
    public void taskBack(final Context context, String taskId, String comment, String assigneeId, String activityId, String procInsId) {
        RetrofitManager.create(ApiService.class, Constant.baseUrl)
                .taskBack(taskId,comment,assigneeId,activityId,procInsId)
                .compose(RxSchedulers.<BaseBean>applySchedulers())
                .compose(mView.<BaseBean>bindToLife())
                .subscribe(new Consumer<BaseBean>() {
                    @Override
                    public void accept(BaseBean dataResponse) throws Exception {
                        Log.e(Constant.tag,dataResponse.getCode()+"");
                        if (dataResponse.getCode() == 201){
                            LoginActivity.start(context);
                            mView.showFaild("会话过期请重新登录");
                        }else {
                            mView.taskBack(dataResponse.getCode());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild(throwable.getMessage());
                    }
                });
    }
}
